// import parseTime, formatTime and set to filter

/**
 * Show plural label if time is plural number
 * @param {number} time
 * @param {string} label
 * @return {string}
 */
function pluralize(time, label) {
  if (time === 1) {
    return time + label
  }
  return time + label + 's'
}

/**
 * @param {number} time
 */
export function timeAgo(time) {
  const between = Date.now() / 1000 - Number(time)
  if (between < 3600) {
    return pluralize(~~(between / 60), ' minute')
  } else if (between < 86400) {
    return pluralize(~~(between / 3600), ' hour')
  } else {
    return pluralize(~~(between / 86400), ' day')
  }
}

/**
 * Number formatting
 * like 10000 => 10k
 * @param {number} num
 * @param {number} digits
 */
export function numberFormatter(num, digits) {
  const si = [
    { value: 1E18, symbol: 'E' },
    { value: 1E15, symbol: 'P' },
    { value: 1E12, symbol: 'T' },
    { value: 1E9, symbol: 'G' },
    { value: 1E6, symbol: 'M' },
    { value: 1E3, symbol: 'k' }
  ]
  for (let i = 0; i < si.length; i++) {
    if (num >= si[i].value) {
      return (num / si[i].value).toFixed(digits).replace(/\.0+$|(\.[0-9]*[1-9])0+$/, '$1') + si[i].symbol
    }
  }
  return num.toString()
}

/**
 * 10000 => "10,000"
 * @param {number} num
 */
export function toThousandFilter(num) {
  return (+num || 0).toString().replace(/^-?\d+/g, m => m.replace(/(?=(?!\b)(\d{3})+$)/g, ','))
}

/**
 * Upper case first char
 * @param {String} string
 */
export function uppercaseFirst(string) {
  return string.charAt(0).toUpperCase() + string.slice(1)
}

/**
 * datetime 2019-01-01 00:00:00
 * @param {String} string
 */
export function datetimeFormatter(date) {
  if (date==null) {
    return null;
  }
  date = new Date(date)
  let fmt = "YYYY-mm-dd HH:MM:SS"
  let ret;
  const opt = {
    "Y+": date.getFullYear().toString(),        // 年
    "m+": (date.getMonth() + 1).toString(),     // 月
    "d+": date.getDate().toString(),            // 日
    "H+": date.getHours().toString(),           // 时
    "M+": date.getMinutes().toString(),         // 分
    "S+": date.getSeconds().toString()          // 秒
    // 有其他格式化字符需求可以继续添加，必须转化成字符串
  };
  for (let k in opt) {
    ret = new RegExp("(" + k + ")").exec(fmt);
    if (ret) {
      fmt = fmt.replace(ret[1], (ret[1].length == 1) ? (opt[k]) : (opt[k].padStart(ret[1].length, "0")))
    };
  };
  return fmt;
}

/**
 * datetime 2019-01-01 00:00:00
 * @param {String} string
 */
export function dateFormatter(date) {
  if (date==null) {
    return null;
  }
  date = new Date(date)
  let fmt = "YYYY-mm-dd"
  let ret;
  const opt = {
    "Y+": date.getFullYear().toString(),        // 年
    "m+": (date.getMonth() + 1).toString(),     // 月
    "d+": date.getDate().toString()            // 日
    // 有其他格式化字符需求可以继续添加，必须转化成字符串
  };
  for (let k in opt) {
    ret = new RegExp("(" + k + ")").exec(fmt);
    if (ret) {
      fmt = fmt.replace(ret[1], (ret[1].length == 1) ? (opt[k]) : (opt[k].padStart(ret[1].length, "0")))
    };
  };
  return fmt;
}

export function thumbnail(url,w,h) {
  if(url == null || url == 'null' || url == undefined || url == '' || url == 'undefined'){
    return "";
  }
  h = Math.round(h);
  w = Math.round(w);
  if (url.substring(0,11) == "http://cdnx") {
    return url+"?x-oss-process=image/resize,m_fill,w_"+w+",h_"+h+"/quality,q_90";
  } else
  if (url.substring(0,10) == "http://cdn") {
    return url+"@"+w+"w_"+h+"h_1e_1c_100Q";
  } else {
    return url;
  }
}
export function thumbnail_pad(url,w,h) {
  if(url == null || url == 'null' || url == undefined || url == '' || url == 'undefined'){
    return "";
  }
  h = Math.round(h);
  w = Math.round(w);
  if (url.substring(0,11) == "http://cdnx") {
    return url+"?x-oss-process=image/resize,m_pad,w_"+w+",h_"+h+"/quality,q_90";
  } else
  if (url.substring(0,10) == "http://cdn") {
    return url+"@"+w+"w_"+h+"h_1e_1c_100Q";
  } else {
    return url;
  }
}
export function thumbnail_cover(url,w) {
  if (w>1080) {
    w = 1080
  }
  w = Math.round(w);
  if (url.substring(0,11) == "http://cdnx") {
    return url+"?x-oss-process=image/resize,w_"+w+"/quality,q_90";
  } else
  if (url.substring(0,10) == "http://cdn") {
    return url+"@"+w+"w_1e_1c_100Q";
  } else {
    return url;
  }
}
